<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm" :inline="true" :model="queryForm">
        <el-form-item :label="dataList.PlanNumber" prop="num">
          <el-input
            v-model="queryForm.num"
            :placeholder="dataList.PlanNumber"
          />
        </el-form-item>
        <el-form-item :label="dataList.PlanName" prop="name">
          <el-input v-model="queryForm.name" :placeholder="dataList.PlanName" />
        </el-form-item>
        <el-form-item :label="dataList.Status" prop="statusId">
          <el-select
            v-model="queryForm.statusId"
            :placeholder="dataList.Placeholder"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="dataList.CreateTime" prop="createTime">
          <el-date-picker
            v-model="queryForm.createTime"
            type="daterange"
            :range-separator="dataList.To"
            :start-placeholder="dataList.StartDate"
            :end-placeholder="dataList.EndDate"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item :label="dataList.JoinDevice" prop="deviceNameNum">
          <el-input
            v-model="queryForm.deviceNameNum"
            :placeholder="dataList.EnterEquipName"
          />
        </el-form-item>
      </el-form>

      <div class="base-content-operate">
        <el-button type="primary" @click="onQuery">{{ btn.Query }}</el-button>
        <el-button @click="cancelForm">{{ btn.Reset }}</el-button>
        <el-button icon="el-icon-plus" type="primary" @click="onNew">{{
          btn.Add
        }}</el-button>
        <el-button icon="el-icon-plus" type="primary" @click="copy">{{
          btn.Copy
        }}</el-button>
      </div>

      <el-table
        ref="DeviceMaintenancePlan"
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column align="center" :label="dataList.Number" width="80">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.PlanNumber">
          <template slot-scope="scope">
            <el-link
              size="mini"
              type="primary"
              @click="particulars(scope.row)"
              >{{ scope.row.num }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column :label="dataList.PlanName">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.PlanendDate">
          <template slot-scope="scope">
            {{ formatTime(scope.row.endDate) }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.Status">
          <template slot-scope="scope">
            {{ scope.row.statusName }}
          </template>
        </el-table-column>
        <!-- <el-table-column :label="dataList.EffectiveDate">
          <template slot-scope="scope">
            {{ scope.row.assertDate }}
          </template>
        </el-table-column> -->
        <el-table-column :label="dataList.Creator">
          <template slot-scope="scope">
            {{ scope.row.creatorName }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.CreateTime">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.EffectiveDate">
          <template slot-scope="scope">
            {{ scope.row.statusId == 30 ? scope.row.assertDate : "" }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.Operation">
          <template slot-scope="scope">
            <div
              v-if="scope.row.statusId == 10 || scope.row.statusId == 40"
              class="base-table-operation"
            >
              <el-link
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)"
                >{{ btn.Edit }}</el-link
              >
              <el-link
                size="mini"
                type="danger"
                @click="handleRepeal(scope.row)"
                >{{ btn.Nullify }}</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :current-page="pagination.pageIndex"
        :page-count="pagination.total"
        @changePage="changePage"
        @sizeChange="sizeChange"
      />
    </div>

    <DeviceMaintenancePlanAddDialog
      v-if="dialogFormAddVisible"
      ref="DeviceMaintenancePlanAdd"
      :copy-id.sync="copyId"
      :dialog-form-visible.sync="dialogFormAddVisible"
    />

    <DeviceMaintenancePlanEditDialog
      v-if="dialogFormVisibleEdit"
      :id.sync="id"
      ref="DeviceMaintenancePlanEdit"
      :dialog-form-visible.sync="dialogFormVisibleEdit"
    />
    <DeviceMaintenancePlanViewDialog
      v-if="dialogFormVisibleView"
      :id.sync="id"
      ref="DeviceMaintenancePlanView"
      :dialog-form-visible.sync="dialogFormVisibleView"
    />
  </div>
</template>

<script>
import { getList, updateStatus } from "../../../api/DeviceMaintenance.js";
import DeviceMaintenancePlanAddDialog from "./DeviceMaintenancePlanAddDialog.vue";
import DeviceMaintenancePlanEditDialog from "./DeviceMaintenancePlanEditDialog.vue";
import { maintenanceTaskPlanStatus } from "../../../utils/const";
import DeviceMaintenancePlanViewDialog from "./DeviceMaintenancePlanViewDialog.vue";
import { formatTimeMin, formatTimeMax, formatTime } from "../../../utils/time";

export default {
  name: "DeviceMaintenancePlanList",
  components: {
    DeviceMaintenancePlanAddDialog,
    DeviceMaintenancePlanEditDialog,
    DeviceMaintenancePlanViewDialog,
  },
  data() {
    return {
      formatTime,
      copyId: "0",
      tableData: [],
      loading: true,
      list: [],
      options: maintenanceTaskPlanStatus,
      queryForm: {
        stateCreateTime: null,
        endCreateTime: null,
        createTime: [],
        num: "",
        name: "",
        statusId: -1,
        deviceNameNum: "",
        departId: sessionStorage.getItem("currentDepartmentId"),
      },
      id: 0,
      dialogFormAddVisible: false,
      dialogFormVisibleEdit: false,
      dialogFormVisibleView: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  computed: {
    btn() {
      return this.$t("ModuleDevice.buttons");
    },
    dataList() {
      return this.$t("ModuleDevice.Device");
    },
  },
  created() {
    this.getList();
  },
  methods: {
    handleSelectionChange(val) {
      this.tableData = val;
    },
    // 详情
    particulars(rowData) {
      this.id = rowData.id;
      this.dialogFormVisibleView = true;
    },
    // 复制
    copy() {
      var rows = this.$refs.DeviceMaintenancePlan.selection;
      if (rows == null || rows.length <= 0) {
        this.$message.warning(this.$i18n.t("ModuleDevice.Message.CopyTip"));
      } else {
        this.copyId = rows[0].id;
        this.dialogFormAddVisible = true;
      }
    },
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.queryForm.stateCreateTime = null;
      this.queryForm.endCreateTime = null;
      this.pagination.pageIndex = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      // this.queryForm.departId =  sessionStorage.getItem("currentDepartmentId");
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.dialogFormAddVisible = true;
    },
    handleRepeal(rowData) {
      this.$confirm(
        this.$t("ModuleDevice.Message.ConfirmNullify"),
        this.$t("ModuleDevice.buttons.Tips"),
        {
          confirmButtonText: this.$t("ModuleDevice.buttons.Confirm"),
          cancelButtonText: this.$t("ModuleDevice.buttons.Cancel"),
          type: "warning",
          center: true,
        }
      )
        .then(() => {
          updateStatus({
            id: rowData.id,
            statusId: 70,
            statusName: this.dataList.Invalid,
          })
            .then((res) => {
              this.getList();
            })
            .catch((res) => {});
        })
        .catch(() => {
          // 用户点击取消按钮或点击遮罩层后的回调函数
          // 可以不做任何操作
        });
    },
    handleEdit(rowData) {
      this.id = rowData.id;
      this.dialogFormVisibleEdit = true;
    },
    onQuery() {
      this.pagination.pageIndex = 1;
      this.queryForm.stateCreateTime = formatTimeMin(
        this.queryForm.createTime[0] ? this.queryForm.createTime[0] : null
      );
      this.queryForm.endCreateTime = formatTimeMax(
        this.queryForm.createTime[1] ? this.queryForm.createTime[1] : null
      );
      this.getList();
    },
  },
};
</script>
